package com.lzh.dlykserver.controller;

import com.lzh.dlykserver.query.TranQuery;
import com.lzh.dlykserver.result.R;
import com.lzh.dlykserver.service.TranService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class TranController {

    private final TranService tranService;


    /**
     * 客户转换交易
     * @param tranQuery
     * @param token
     * @return
     */
    @PostMapping("/api/customer/tran")
    public R addTran(@RequestBody TranQuery tranQuery,
                     @RequestHeader("Authorization") String token) {
        // 检查该客户是否已经转换为交易
        int count = tranService.countByCustomerId(tranQuery.getCustomerId());
        if (count > 0) {
            return R.FAIL("该客户已转换为交易，不能重复转换");
        }
        tranQuery.setToken(token);
        Boolean isConvert = tranService.insertTran(tranQuery);
        return isConvert ? R.OK() : R.FAIL();
    }
}
